package com.sbm.springsecurity;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUser implements UserDetails {

	private static final long serialVersionUID = 1L;

	private SecurityUserInterface user;

	public SecurityUser(SecurityUserInterface user) {
		this.user = user;
	}

	public SecurityUserInterface getUser() {
		return user;
	}

	public void setUser(SecurityUserInterface user) {
		this.user = user;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new ArrayList<>();
		for (SecurityRoleInterface role : this.user.getRoles()) {
			for (SecurityPermissionInterface permission : role.getPermissions()) {
				SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission.getName());
				authorities.add(authority);
			}
		}
		return authorities;
	}

	@Override
	public String getPassword() {
		return this.user.getPassword();
	}

	@Override
	public String getUsername() {
		return this.user.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
